package com.twu.biblioteca.service;
import com.twu.biblioteca.dao.BookDaoStrategy;
import com.twu.biblioteca.dao.ItemDao;
import com.twu.biblioteca.dao.MovieDaoStrategy;
import com.twu.biblioteca.exception.CheckoutFailedException;

import java.io.PrintStream;

public class MenuService {
    public static final int INDEX_PRINT_MENU = 1;
    public static final int INDEX_PRINT_BOOK_LIST = 2;
    public static final int INDEX_CHECKOUT_BOOK = 3;
    public static final int INDEX_RETURN_BOOK = 4;
    public static final int INDEX_PRINT_MOVIE_LIST = 5;
    public static final int INDEX_CHECKOUT_MOVIE = 6;
    public static final int INDEX_RETURN_MOVIE = 7;

    private final String MENU_BEGIN = "-----Menu Begin-----\n";
    private final String MENU_END = "------Menu End------\n";
    private final String PROMPT_INVALID_INPUT = "Invalid input, please try again: \n";

    private ItemService bookService;
    private ItemService movieService;

    private PrintStream printStream = System.out;

    public MenuService(){
        bookService = new ItemService(new ItemDao(new BookDaoStrategy()));
        movieService = new ItemService(new ItemDao(new MovieDaoStrategy()));
    }

    public enum MENU {
        LIST_BOOKS(INDEX_PRINT_BOOK_LIST, "List Books"),
        CHECKOUT_BOOK(INDEX_CHECKOUT_BOOK, "Checkout Book"),
        RETURN_BOOK(INDEX_RETURN_BOOK, "Return Book"),
        LIST_MOVIES(INDEX_PRINT_MOVIE_LIST, "List Movies"),
        CHECKOUT_MOVIE(INDEX_CHECKOUT_MOVIE,"Checkout Movie"),
        RETURN_MOVIE(INDEX_RETURN_MOVIE,"Return Movie");
        private int index;

        private String description;

        private MENU(int index, String description) {
            this.index = index;
            this.description = description;
        }

        public int getIndex() {
            return index;
        }

        public String getDescription() {
            return description;
        }

    };

    public void printMenu() {
        printStream.print(MENU_BEGIN);
        for (MenuService.MENU menu : MenuService.MENU.values()) {
            printStream.println(String.format(" %s : %s", menu.getIndex(), menu.getDescription()));
        }
        printStream.print(MENU_END);
    }

    public void handle(int input) throws CheckoutFailedException {
        switch (input) {
            case INDEX_PRINT_MENU:
                printMenu();
                break;
            case INDEX_PRINT_BOOK_LIST:
                bookService.printAvailableItemList();
                break;
            case INDEX_CHECKOUT_BOOK:
                bookService.printAvailableItemList();
                bookService.handleCheckoutItem();
                break;
            case INDEX_RETURN_BOOK:
                bookService.printUnavailableItemList();
                bookService.handleReturnItem();
                break;
            case INDEX_PRINT_MOVIE_LIST:
                movieService.printAvailableItemList();
                break;
            case INDEX_CHECKOUT_MOVIE:
                movieService.printAvailableItemList();
                movieService.handleCheckoutItem();
                break;
            case INDEX_RETURN_MOVIE:
                movieService.printUnavailableItemList();
                movieService.handleReturnItem();
                break;
            default:
                handleDefault();
                break;
        }
    }

    private void handleDefault() {
        this.printStream.print(PROMPT_INVALID_INPUT);
    }

    public void setPrintStream(PrintStream printStream) {
        this.printStream = printStream;
    }

    public void setBookService(ItemService bookService) {
        this.bookService = bookService;
    }
}
